#ifndef RoomStructEngine_H
#define RoomStructEngine_H

#include <QObject>
#include <QList>
#include <QVector3D>
#include <QtQmlIntegration>

//房间系统引擎，用于生成多个房间之间的关系
namespace RoomEngine
{
class Room;

class Port:public QObject//与其它房间的通路
{
    Q_OBJECT
    QML_ELEMENT
public:
    Port();

    QVector3D fromPos() const;
    void setFromPos(const QVector3D &newFromPos);

    Room *fromRoom() const;
    void setFromRoom(Room *newFromRoom);

    QVector3D toPos() const;
    void setToPos(const QVector3D &newToPos);

    Room *toRoom() const;
    void setToRoom(Room *newToRoom);

signals:
    void fromPosChanged();

    void fromRoomChanged();

    void toPosChanged();

    void toRoomChanged();

private:
    QVector3D m_fromPos;//从哪里来
    Room* m_fromRoom = nullptr;//到哪里去

    QVector3D m_toPos;
    Room* m_toRoom = nullptr;
    Q_PROPERTY(QVector3D fromPos READ fromPos WRITE setFromPos NOTIFY fromPosChanged FINAL)
    Q_PROPERTY(Room *fromRoom READ fromRoom WRITE setFromRoom NOTIFY fromRoomChanged FINAL)
    Q_PROPERTY(QVector3D toPos READ toPos WRITE setToPos NOTIFY toPosChanged FINAL)
    Q_PROPERTY(Room *toRoom READ toRoom WRITE setToRoom NOTIFY toRoomChanged FINAL)
};

class Room:public QObject//房间
{
    Q_OBJECT
    QML_ELEMENT

public:
    enum Type{//房间的类型
        None,//不存在的房间
        Start,//出生点房间
        Normal,//普通房间
        End//结束房间
    };
    Q_ENUM(Type);

public:
    Room();

    QVector3D position() const;
    void setPosition(const QVector3D &newPosition);

    QList<Port *> ports() const;
    void setPorts(const QList<Port *> &newPorts);

    Type type() const;
    void setType(const Type &newType);

signals:
    void positionChanged();

    void portsChanged();

    void typeChanged();

private:
    QVector3D m_position;//房间所在的位置
    QList<Port*> m_ports;//房间的出口
    Type m_type = None;

    Q_PROPERTY(QVector3D position READ position WRITE setPosition NOTIFY positionChanged FINAL)
    Q_PROPERTY(QList<Port *> ports READ ports WRITE setPorts NOTIFY portsChanged FINAL)
    Q_PROPERTY(Type type READ type WRITE setType NOTIFY typeChanged FINAL)
};

class RoomStructEngine:public QObject
{
    Q_OBJECT
    QML_ELEMENT
public:
    RoomStructEngine();

    QList<Room *> rooms() const;
    void setRooms(const QList<Room *> &newRooms);

    int seed() const;
    void setSeed(int newSeed);

    QVector3D extend() const;
    void setExtend(const QVector3D &newExtend);

    Q_INVOKABLE void generate();//生成房间
signals:
    void roomsChanged();

    void seedChanged();

    void extendChanged();

private:
    QList<QVector3D> generateNormalRoom(QVector3D start,QVector3D end);
    void findWay(QList<QVector3D>& ways,const QVector3D& end);
private:
    QList<Room*> m_rooms;
    QVector3D m_extend;//房间规模
    int m_seed = 0;

    Q_PROPERTY(QList<Room *> rooms READ rooms WRITE setRooms NOTIFY roomsChanged FINAL)
    Q_PROPERTY(int seed READ seed WRITE setSeed NOTIFY seedChanged FINAL)
    Q_PROPERTY(QVector3D extend READ extend WRITE setExtend NOTIFY extendChanged FINAL)
};

};

#endif
